home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / LSC_MACW / MWR.H < prev   
Text File  |  1988-12-03  |  15KB  |  342 lines

  1. /*-----------------------------------------------------------------------
  2.  MacWrite 4.5 document file and clipboard format.
  3.  After Macintosh Technical Notes #12 (version of 20 Feb 1986) and #13;
  4.  you will need those notes to understand this file.
  5.  
  6.  Nov '88 written by Dan Kegel (D.R.KEGEL @ GENIE).
  7.  Assumes an enum with a range of (-1,0) is stored in a char (as in LSC3.0).
  8.  
  9.  MacWrite 4.5 documents begin with a copy of the structure mwr_filehdr_t.
  10. ------------------------------------------------------------------------*/
  11.  
  12. /*--- MacWrite signatures ----------------------------------------------*/
  13. #define MWR_FILECREATOR 'MACA'
  14. #define MWR_FILETYPE    'WORD'
  15. #define MWR_SCRAPTYPE   'MWRT'    /* also writes out TEXT and/or PICT types */
  16.  
  17. #define MWR_COMPRESS_RTYPE 'STR '
  18. #define MWR_COMPRESS_RID 700
  19.  
  20. /*--- Text justification methods ---------------------------------------*/
  21. #define MWR_JUST_LEFT 0
  22. #define MWR_JUST_CENT 1
  23. #define MWR_JUST_RITE 2
  24. #define MWR_JUST_FULL 3
  25.  
  26. /*--- A boolean type used in other structures --------------------------*/
  27. typedef enum {
  28.     MWR_FALSE = 0,
  29.     MWR_TRUE = -1                /* or 0xff, depending on compiler...    */
  30. } mwr_boolean;                    /* note: sizeof(mwr_boolean) must be 1  */
  31.  
  32. /*--- The Information Array --------------------------------------------------
  33.  There is an information array for each of the footer, header, and main 
  34.  document.  For each paragraph there is one element in the array.
  35.  (Not to be confused with the Line Height array, which is organized
  36.  similarly, but has variable-sized elements and is harder to access.)
  37. -----------------------------------------------------------------------------*/
  38. typedef struct {
  39.     short height;    /* Height of paragraph in pixels.  NOTE: Positive 
  40.                      * means text, negative means a picture, and 
  41.                      * zero means a ruler.
  42.                      */
  43.     short page_pos;    /* Distance from top of page, in pixels. */
  44.  
  45.     char page_num;    /* Page number the paragraph begins on. */
  46.     char dummy[3];    /* unused */
  47.  
  48.     /* Paragraph status byte & file position are packed into a long:    */
  49.     long
  50.         dirty_bit    : 1,        /* unused             [high bit of long]    */
  51.         use_just_code_bit : 1,    /* 1-> use just_code; 0-> use ruler        */
  52.         in_ram_bit    : 1,        /* unused                                */
  53.         on_way_bit    : 1,        /* unused                                */
  54.         compress_bit: 1,        /* set if paragraph compressed on disk    */
  55.         inuse_bit    : 1,        /* unused                                */
  56.         just_code    : 2,        /* MWR_JUST_[LEFT/CENT/RITE/FULL]        */
  57.         
  58.         file_pos    : 24;        /* offset of paragraph data in file        */
  59.  
  60.     /* Paragraph data size in bytes, including text, format runs, etc. */
  61.     short nbytes;
  62.  
  63.     /* Common format information of the paragraph for menu checking.
  64.      * I don't understand this.
  65.      */
  66.     short
  67.         font    : 5,    /* index into font table; 0 = 'no common font'? */
  68.         size    : 3,    /* values 0-7 mean: other/9/10/12/14/18/24 pt */
  69.         /* Styles byte: bits 0..7 are 
  70.          *   plain/bold/italic/underline/outline/shadow/super/subscript
  71.          * All bits zero means 'no common style'.
  72.          */
  73.         styles    : 8;
  74. } mwr_para_info_t, *mwr_para_info_p, **mwr_para_info_h;
  75.  
  76.  
  77. /*--- The Window description structure ---------------------------------
  78.  The main file header contains three of these, one for the header,
  79.  one for the footer, and another for the main document.
  80. ------------------------------------------------------------------------*/
  81. typedef struct {
  82.     /* Start of selection */
  83.     short sel_start_para;    /* 0 paragraph number */
  84.     short sel_start_char;    /* 2 INCLUSIVE character position */
  85.  
  86.     /* End of selection */
  87.     short sel_end_para;        /* 4 paragraph number */
  88.     short sel_end_char;        /* 6 EXCLUSIVE character position */
  89.  
  90.     short vert_offset;        /* 8 The negative difference between the top of the
  91.                              * page and the top of the screen in pixels.  
  92.                              * Zero if top of the page is at top of screen.
  93.                              */
  94.  
  95.     short need_redraw_para;    /* 10 First paragraph to redraw document from
  96.                              * when first loaded.
  97.                              */
  98.  
  99.     /* For each paragraph in this window (header, footer, or main document)
  100.      * there is one element in the Information Array, describing the font,
  101.      * size, style, and contents of each paragraph.
  102.      * Each element is an mwr_para_info_t; these are fixed-size,
  103.      * in contrast to the elements of the Line Height array (below).
  104.      */
  105.     long info_pos;            /* 12 Ptr to this document's information array. */
  106.     short info_len;            /* 16 Length of same, in bytes. */
  107.  
  108.     /* For each paragraph in this window (header, footer, or main document)
  109.      * there is one element in the line height array, as follows:
  110.      * Bytes    Item
  111.      * 0-1    Number of bytes of line height data for this paragraph (n).
  112.      * 2-n    Line height information.  
  113.      * The format of the information is as follows:
  114.      *    One byte for the height of a line (in pixels), followed by an 
  115.      *    optional byte repeat factor equal to the number of lines 
  116.      *    (including the first line) with the same height.  The repeat 
  117.      *    factor is distinguished from a line height by having its high 
  118.      *    bit set, thus limiting any line's height to a maximum of 127 
  119.      *    pixels, and any repeat factor to 127.  The line height information 
  120.      *  for the next paragraph always starts on a word boundary.
  121.      */
  122.     long lineheight_pos;    /* 18 Ptr to the line height array. */
  123.     short lineheight_len;    /* 22 Length of same, in bytes. */
  124.  
  125.     /* The following three fields are unused in the main document.
  126.      * They give the coordinates of the top left corner of header/footer icons
  127.      * relative to the top left corner of the ruler.
  128.      */
  129.     Point pagenum_pos;        /* 24 top left of page # icon */
  130.     Point date_pos;            /* 28 top left of date icon */
  131.     Point time_pos;            /* 32 top left of time icon */
  132.  
  133.     long dummy;                /* 36 unused */
  134.  
  135.     /* "oval" here might mean "time & date icons". */
  136.     mwr_boolean oval_redraw;        /* 40 TRUE -> redraw ovals. */
  137.     mwr_boolean last_oval_update;    /* 41 TRUE -> ruler heights are included in
  138.                                      * oval positions (?)
  139.                                      */
  140.     short style;            /* 42 The face that was active at time of the save. */
  141.     short font;                /* 44 The font that was active at time of the save. */
  142. } mwr_win_t;
  143.  
  144. /*--- The main file header ----------------------------------------------
  145.  Every MacWrite document file starts with this structure, which describes
  146.  the document in terms of windows and paragraphs.
  147.  Paragraph data can be scattered all over the file, and there can even
  148.  be unused regions in the file!
  149. -------------------------------------------------------------------------*/
  150. typedef struct {
  151.     short    macwrite_version;    /* 6 if MacWrite 4.5, 3 if MacWrite 2.2 */
  152.                                 /* MacWrite 2.2 format is quite different. */
  153.  
  154.     short doc_nparagraphs,        /* 2 # of paragraphs in main document,    */
  155.           hdr_nparagraphs,        /* 4 header, and footer, respectively.    */
  156.           ftr_nparagraphs;        /* 6 Each is >= 2 (1 for ruler, 1 for cursor). */
  157.  
  158.     mwr_boolean title_page;        /* 8 FALSE -> draw 1st page headers & footers. */
  159.     mwr_boolean dummy1;            /* 9 Unused */
  160.     mwr_boolean disp_scrap;        /* 10 TRUE -> display scrap.  */
  161.     mwr_boolean disp_ftr;        /* 11 TRUE -> display footer. */
  162.     mwr_boolean disp_hdr;        /* 12 TRUE -> display header. */
  163.     mwr_boolean disp_rulers;    /* 13 TRUE -> display rulers. */
  164.  
  165.     short active_doc;    /* 14 Document active at time of save:
  166.                             0 = Main Document
  167.                             1 = Header
  168.                             2 = Footer
  169.                             -1($FFFF) = whole document needs to be redrawn
  170.                          */
  171.     short start_page_num;    /* 16 The offset for the page numbers. */
  172.  
  173.  
  174.     /* Each element in the free list array consists of the following:
  175.      *  Bytes    Item
  176.      *  0-3    Position - Corresponds to the file position of this free block.
  177.      *  4-7    Free Bytes - The size of this block in bytes.
  178.      */
  179.     long  freelist_pos;        /* 18 Ptr to the free list of unused space. */
  180.     short freelist_len;        /* 22 Actual length of free list (<= freelist_alloc) */
  181.     short freelist_alloc;    /* 24 # of bytes allocated for free list on disk */
  182.  
  183.     char dummy2[14];        /* 26 unused */
  184.  
  185.     char univ_print_rec[120];    /* 40 Universal printing record. */
  186.  
  187.     mwr_win_t ftr_win;        /* 160 Description of status of footer, header, and */
  188.     mwr_win_t hdr_win;        /* 206 main document windows, respectively. */
  189.     mwr_win_t doc_win;        /* 252 */
  190. } mwr_filehdr_t, *mwr_filehdr_p, **mwr_filehdr_h;
  191.  
  192.  
  193. /*---- A Placeholder -----------------------------------------------------
  194.  This structure holds an opened MacWrite file header.
  195.  I have only defined the fields that will be useful for converting to
  196.  TEXT format.
  197. --------------------------------------------------------------------------*/
  198. typedef struct {
  199.     int refNum;                    /* for calls to FSRead() */
  200.     mwr_filehdr_t header;
  201.     mwr_para_info_h hParaInfo;
  202.     char compressTab[16];
  203.     char name[128];
  204. } mwr_opened_t, *mwr_opened_p, **mwr_opened_h;
  205.     
  206. /*---- Paragraph Data -------------------------------------------------------
  207.  Paragraphs are not stored in sequential order, but rather are pointed
  208.  to by the Paragraph Data Position and the Paragraph Data Length.
  209.  
  210.  Ñ Ruler Paragraph:
  211.    A ruler paragraph is simply a mwr_ruler_t.
  212.  
  213.  Ñ Picture Paragraph:  
  214.    A picture paragraph starts with a rectangle containing the picture size,
  215.    followed by a standard Quickdraw picture.  
  216.  
  217.  Ñ Page break Paragraph:
  218.    A page break is also a picture, but the ASCII characters for MAGICPIC 
  219.    replace the rectangle at the start of the picture.
  220.  
  221.  Ñ Text Paragraph:
  222.     Bytes    Item
  223.     0-1        Text length in bytes (n). If text is compressed, (n) refers to the 
  224.     number of uncompressed characters.
  225.  
  226.     2-(n+2)    Decompressed or Compressed Text :
  227.         Decompressed -  ASCII data.
  228.         Compressed - See Compression Scheme below.  Ends
  229.         on an even word boundary.
  230.  
  231.     On an even word boundary following the text is a word for the 
  232.     byte length of the format run, which is an array of mwr_text_format.
  233.  
  234.  Compression Scheme - In the document's resources is a resource of type STR 
  235.  with the ID #700.  It contains the fifteen most common letters in the 
  236.  language of the MacWrite that created the document.  
  237.  The compressed paragraph  is a collection of nibbles (four bits, or half-bytes).
  238.  If a letter is one of the fifteen most common then it only takes a nibble to 
  239.  represent.  Otherwise, a nibble equal to fifteen signifies that the letter 
  240.  following is a byte of standard ASCII. (Important note:  the ASCII byte will 
  241.  not necessarily be aligned on a byte boundary.)  
  242.  For example, if the first five most common letters in the language are 
  243.  M, c, W,  i, and e then the sequence of nibbles 1, 15, 6, 1, 2, 3, 15, 7, 2, 4, 
  244.  15, 7, 4, 5 spells out MacWrite (assuming, of course, that the letters a, r, 
  245.  and t are not in the list of most common letters).
  246. ----------------------------------------------------------------------------*/
  247. typedef struct {
  248.     short char_offset;    /* position of 1st char to which 
  249.                          * this format change applies. */
  250.     char point_size;
  251.     char style;            /* bit 0..6 = bold/italic/under/over/shadow/super/sub */
  252.     short font_num;
  253. } mwr_text_format;
  254.  
  255. typedef struct {
  256.     short left_margin;    /* units of 1/80" measured from left edge of ruler;
  257.                          * ruler starts at 1", so value of 0 means 1" margin. */
  258.     short right_margin;    /* from left size of screen to right margin. */
  259.     
  260.     char just_code;        /* MWR_JUST_[LEFT/CENT/RITE/FULL] */
  261.     
  262.     char ntabs;            /* # of tabs present, from zero to ten. */
  263.  
  264.     short
  265.         six_lpi_bit : 1,        /* set if 6 lines per inch is active. */
  266.         spacing_code : 15;        /* spacing = 1 + (spacing_code / 2) */
  267.  
  268.     short first_line_indent;    /* From left edge of screen, in pixels (?). */
  269.  
  270.     short tab_array[10];        /* From left edge of screen, in pixels. 
  271.                                  * Decimal tabs are negative.
  272.                                  */
  273.     long dummy;                    /* Unused. */
  274. } mwr_ruler_t;
  275.  
  276.  
  277. /*---- MacWrite Clipboard Format --------------------------------------------
  278.  MacWrite's custom resource type is called 'MWRT'.  
  279.  
  280.  If the clipboard contains a single picture, 
  281.  then only the standard ResType 'PICT' is used.  
  282.  If it contains any text paragraphs, 
  283.  then that text will be written out using the standard ResType 'TEXT'.  
  284.  Note that this type consists of standard ASCII characters, and 
  285.  formatting information is not included. 
  286.  The 'MWRT' custom scrap is written out only if there is room in memory 
  287.  and on disk.
  288.  
  289.  'MWRT' Variables - Each 'MWRT' ResType begins with a mwr_scraphdr_t,
  290.  and continues with an array of mwr_scrap_para_t, one per paragraph.
  291.  
  292.  The documentation appears to imply that MacWrite does not compress text
  293.  stored in the scrap.
  294.  
  295.  Since the desk scrap is kept in a file in the system folder (right?)
  296.  it is safe to assume that any reader of the scrap is using the same system
  297.  folder, and therefore the font mapping does not change.
  298.  Therefore the font mapping STR resource is not placed in the clipboard...
  299. ---------------------------------------------------------------------------*/
  300. typedef struct {
  301.     short nparagraphs;            /* # of paragraphs in the clipboard. */
  302.     mwr_boolean start_on_word;    /* clipboard contents start on word break */
  303.     mwr_boolean end_on_word;    /* clipboard contents end on word break */
  304.     mwr_boolean start_on_para;    /* clipboard contents start on paragraph break */
  305.     mwr_boolean end_on_para;    /* clipboard contents end on paragraph break */
  306. } mwr_scraphdr_t;
  307.  
  308. typedef struct {
  309.     short nbytes;                /* Length of para_data in bytes. */
  310.     short para_type;            /* > 0 : text, < 0 : pict, 0 : ruler */
  311.     char para_data[/*nbytes*/];    /* nbytes bytes of paragraph data, see above
  312.                                  * for format info.  Includes font, style, and
  313.                                  * size info for text paragraphs.
  314.                                  */
  315. } mwr_scrap_para_t;                /* variable-length */
  316.  
  317.  
  318. /*---- Font Mapping --------------------------------------------------------
  319.  In the document's resources is a resource of type STR with the ID #801. 
  320.  It contains a mapping of fonts to font resource IDs and information 
  321.  on real fonts. 
  322.  This resource begins with a word which represents the number of fonts in
  323.  the document.  That word is followed by a series of words, one for each font,
  324.  which contain the font number of the font in the system file the document was 
  325.  last saved under.  These words are in order of appearance of the font in the 
  326.  font menu.  These words are followed by a series of bytes, one for each font.  
  327.  The byte indicates which sizes of the font are contained in the system file.  
  328.  MacWrite supports fonts of sizes 9,10,12,14,18,24.  
  329.  Bit 0 of the byte is set if size 9 of the font is real, 
  330.  bit 1 if size 10 is real and so-on to bit 5, which is set if size 24 is real. 
  331. -----------------------------------------------------------------------------*/
  332.  
  333. /*------------------------------------------------------------------------
  334.  Function prototypes.
  335. -------------------------------------------------------------------------*/
  336. OSErr mwr_ReadParagraph(mwr_opened_t *, int, Handle);
  337. int   mwr_unpack(mwr_para_info_t **, int, Handle, Handle, char *);
  338. OSErr mwr_OpenFile(char *filename, int, mwr_opened_t *);
  339. void  mwr_CloseFile(mwr_opened_t *);
  340. /* hack to allow unsigned char * or char * first arg to mwr_TEReadFile */
  341. OSErr mwr_TEReadFile(void *, int, TEHandle);
  342.